package mwa.cs.dao;
import java.util.List;
import java.util.Map;
import mwa.cs.database.Database;
import mwa.cs.model.RequestType;

public class RequestTypeDAO {
	/** a database of this Receive_code */
    private Database db;
    
    /**
     * Instantiate a new object of this class
     * @param database a database
     */
    public RequestTypeDAO(Database db) {
        this.db = db;
    }
    
public List<Map<String, Object>> findRequestTypeByCondition(int pnSiteId,String paCondition) {
    	
        String vaSql = "SELECT * FROM request_type WHERE site_id = "+pnSiteId+" AND "+paCondition;
        List<Map<String, Object>> result = db.queryList(vaSql);
        if (result != null) {
            return result;
        } else {
            return null;
        }
    }

//get request_type 
	 public List<Map<String, Object>> findRequestTypeBySiteId(int pnSiteId) {
	        String vaSql = "SELECT request_type, request_description FROM request_type WHERE site_id = ? AND request_group='0' ORDER BY request_type ASC";
	        List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
	        if (result != null) {
	            return result;
	        } else {
	            return null;
	        }
	    }

		public List<Map<String, Object>> findRequestType(int pnSiteId) {
			String vaSql = "select REQUEST_GROUP,REQUEST_TYPE,REQUEST_DESCRIPTION "
					+ "from REQUEST_TYPE where SITE_ID = ? order by REQUEST_GROUP, REQUEST_TYPE";
			List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
			if (result != null) {
				return result;
			} else {
				return null;
			}
		}

		// insert Louis 21/10/2557
		public void addRequestType(RequestType poReqType) {
			try {
				String vaSql = "insert into REQUEST_TYPE (SITE_ID,REQUEST_GROUP,REQUEST_TYPE,REQUEST_DESCRIPTION,";
				vaSql += "INSERT_USER_ID,INSERT_DATE,INSERT_TIME,UPDATE_USER_ID,UPDATE_DATE,UPDATE_TIME)";
				vaSql += "values (?,?,?,?,?,?,?,?,?,?)";

				db.insert(vaSql, poReqType.getVnSiteId(), poReqType.getVaRequestGroup(),
						poReqType.getVaRequestType(),
						poReqType.getVaRequestDescription(),
						poReqType.getVnInsertUserId(), poReqType.getVaInsertDate(),
						poReqType.getVaInsertTime(), poReqType.getVnInsertUserId(),
						poReqType.getVaInsertDate(), poReqType.getVaInsertTime());

			} catch (Exception e) {
				throw new RuntimeException(e);
			}
		}

		// Update  Louis 21/10/2557
		public int update(RequestType poReqType, String paOldCodeId) {

			String vaSql = " UPDATE REQUEST_TYPE SET REQUEST_TYPE = ? , REQUEST_GROUP = ? , "
					+ " REQUEST_DESCRIPTION = ? , UPDATE_USER_ID = ? , "
					+ " UPDATE_DATE = ? , UPDATE_TIME = ? "
					+ " WHERE REQUEST_TYPE = ? AND SITE_ID = ? ";

			try {
				db.update(vaSql, poReqType.getVaRequestType(),
						poReqType.getVaRequestGroup(),
						poReqType.getVaRequestDescription(),
						poReqType.getVnUpdateUserId(), poReqType.getVaUpdateDate(),
						poReqType.getVaUpdateTime(), paOldCodeId,
						poReqType.getVnSiteId());
				
				return 1;
				
			} catch (Exception e) {
				return -1;
			}
		}
		
	    // Delete Louis 21/10/2557
	    public boolean delete(int pnSiteId,String paRequestType) {
	        String vaSql = "DELETE FROM REQUEST_TYPE WHERE SITE_ID = ? and REQUEST_TYPE = ?";
	        try {
	            db.update(vaSql, pnSiteId,paRequestType);
	            return true;
	        } catch (Exception e) {
	            return false;
	        }
	    }
    
	    public List<Map<String, Object>> findRequestTypeByRequestType(int pnSiteId) {
	        String vaSql = "SELECT request_type,request_description FROM request_type WHERE site_id = ? AND request_group = 0 AND request_type NOT IN 01";
	        List<Map<String, Object>> result = db.queryList(vaSql, pnSiteId);
	        if (result != null) {
	            return result;
	        } else {
	            return null;
	        }
	    }
    
	    // ค้นหารายละเอียดประเภทของคำร้อง หน้า CS65001
	    public String getRequestTypeForAskRequest(int pnSiteId, String paRequestType) {
	        String vaSql = "SELECT request_description FROM request_type WHERE site_id = ? AND request_type = ?";
	        Map<String, Object> result = db.querySingle(vaSql, pnSiteId, paRequestType);
	        if (result != null) {
	            return result.get("REQUEST_DESCRIPTION").toString();
	        } else {
	            return null;
	        }
	    }
}
